// pages/level/index.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    inputViewShow: false,
    editID: 0,
    name: '',
    level: 0,
    annual_fee: 0,
    weigh: 0,
    dataList: [],
    orgArray: [],
    orgNameArray: [],
    orgID: 0,
    orgIndex: 0,
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
    isAddBtnShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    that.doLoadData();
    that.loadOrgData();
  },
  onShow: function () {
    var that = this;
  },
  //加载数据
  doLoadData: function () {
    var that = this;

    var parameter = {};
    parameter['page'] = 1;

    /**
     * 显示 loading
     */
    that.setData({
      page: 1,
      loadinghidden: false,
      hothidden: false,
    });

    http.ApiGet(apiUrl.UrlLevelList, parameter, function (res) {
      wx.stopPullDownRefresh(); //停止下拉刷新
      wx.hideNavigationBarLoading(); //完成停止加载
      that.setData({
        loadinghidden: true
      })
      if (res.result.errorCode == 1) {
        var dataList = res.data;
        if (dataList.length > 0) {
          dataList = dataList.map(function (item) {
            return item;
          })
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: dataList
        });
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.doLoadData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['page'] = page;

    http.ApiGet(apiUrl.UrlLevelList, parameter, function (res) {
      if (res.result.errorCode == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function (item) {
              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  //列表点击
  listCellTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);
  },
  loadOrgData: function () {
    var that = this;

    var parameter = {};
    parameter['page'] = 1;
    //获取数据
    http.ApiGet(apiUrl.UrlOrgList, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.result.errorCode == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          var orgName = [];
          var orgIndex = 0;
          var orgID = newData[0].id;


          newData.map(function (item, index) {
            orgName.push(item.name);
            if (orgID == item.id) {
              orgIndex = index;
            }
          });
          that.setData({
            orgArray: newData,
            orgNameArray: orgName,
            orgID: orgID,
            orgIndex: orgIndex
          });
        }
      }
    });
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var orgArray = this.data.orgArray;
    this.setData({
      orgIndex: e.detail.value,
      orgID: orgArray[e.detail.value].id
    })
  },
  editTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);
    that.setData({
      editID: item.id,
      orgID: item.org_id,
      inputViewShow: true,
      name: item.name,
      level: item.level,
      annual_fee: item.annual_fee,
      weigh: item.weigh,
    });

    var orgIndex = 0;
    var orgID = item.org_id;

    that.data.orgArray.map(function (item, index) {
      if (orgID == item.id) {
        orgIndex = index;
      }
    });

    that.setData({
      orgIndex: orgIndex
    });

  },
  delTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);

    var formData = {};
    formData['id'] = item.id;

    wx.showModal({
      title: '确定删除？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          http.ApiPost(apiUrl.UrlLevelDel, formData, function (res) {
            if (res.result.errorCode == 1) {
              that.doLoadData();
            } else {
              wx.showModal({
                title: res.result.errorMessage
              });
            }
          });
        }
      }
    });
  },
  addBtnTap: function () {
    this.setData({
      editID: 0,
      orgId:0,
      orgIndex:0,
      inputViewShow: true,
      name: '',
      level: 0,
      annual_fee: 0,
      weigh: 0,
    });
  },
  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  levelInput: function (e) {
    this.setData({
      level: e.detail.value
    })
  },
  annualFeeInput: function (e) {
    this.setData({
      annual_fee: e.detail.value
    })
  },
  weighInput: function (e) {
    this.setData({
      weigh: e.detail.value
    })
  },
  doOK: function () {
    var that = this;
    var name = that.data.name;
    if (name == "") {
      wx.showModal({
        title: '名称不能为空'
      });
      return;
    }
    that.closeInput();
    that.doEditPost({
      'id': that.data.editID,
      'org_id': that.data.orgID,
      'name': that.data.name,
      'level': that.data.level,
      'annual_fee': that.data.annual_fee,
      'weigh': that.data.weigh,
    });
  },
  closeInput: function () {
    this.setData({
      inputViewShow: false,
    });
  },
  doEditPost: function (postData) {
    var that = this;

    http.ApiPost(apiUrl.UrlLevelSubmit, postData, function (res) {
      if (res.result.errorCode == 1) {
        wx.hideToast()
        wx.showToast({
          title: '操作成功！',
          icon: 'success',
          duration: 3000
        });
        that.doLoadData();
        setTimeout(function () {
          wx.hideToast();
        }, 1000)
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
})